package com.kosam.commons.base.service;

import com.kosam.commons.base.repository.RepositorySupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;
import java.util.List;

/**
 * Created by tgx on 2017/2/17.
 */
public class ServiceSupportImpl<M, ID extends Serializable> implements ServiceSupport<M, ID> {
    protected RepositorySupport<M, ID> repositorySupport;

    public ServiceSupportImpl() {
    }

    @Autowired
    public void setRepositorySupport(RepositorySupport<M, ID> repositorySupport) {
        this.repositorySupport = repositorySupport;
    }

    public M save(M m) {
        return this.repositorySupport.save(m);
    }

    public M saveAndFlush(M m) {
        m = this.save(m);
        this.repositorySupport.flush();
        return m;
    }

    public void delete(ID id) {
        this.repositorySupport.delete(id);
    }

    public void delete(M m) {
        this.repositorySupport.delete(m);
    }

    public void delete(List<ID> ids) {
        this.repositorySupport.delete((ID) ids);
    }

    public M findOne(ID id) {
        return this.repositorySupport.findOne(id);
    }

    public boolean exists(ID id) {
        return this.repositorySupport.exists(id);
    }

    public long count() {
        return this.repositorySupport.count();
    }

    public List<M> findAll() {
        return this.repositorySupport.findAll();
    }

    public List<M> findAll(Sort sort) {
        return this.findAll(sort);
    }

    @Override
    public Page<M> findAll(Pageable pageable) {
        return this.repositorySupport.findAll(pageable);
    }
}
